import { isError } from "lodash"

function sleep(ms: number) {
    return new Promise(
        resolve => setTimeout(resolve, ms))
}

async function nothrow<T>(promise: Promise<T>) {
    let result: (T | Error)
    try {
        result = await promise
        return result
    } catch (error) {
        if (!error) {
            result = new Error('unknown')
            return result
        }
        if (isError(error)) {
            result = error as Error
            return result
        }
        return new Error(error + '')
    }
}

export { sleep, nothrow }
